home *** CD-ROM | disk | FTP | other *** search
- #include "headers.h"
- #include <drag.h>
- #include "Util.h"
- #include "CContainer.h"
- #include "CSimpleAppSite.h"
- #include "CArgumentParser.h"
- #include "CDragDrop.h"
- #include "App.h"
- #include <ctype.h>
- #include "urlapi.h"
-
- #define DEFAULT_WIDTH 150
- #define DEFAULT_HEIGHT 150
-
- CContainer* gContainer = nil;
- CSimpleAppSite* gSite;
- IControl* gControl;
- CDragDrop* gDragDrop = nil;
- Rect gControlRect = {50, 50, 200, 200};
-
- static void DoContextChange(UInt32 ContextID, ContextCommand Command);
- static void Inval(void);
-
- void InitApp(AppData* AppDataPtr)
- {
- // Initialize ActiveX
- CoInitialize(nil);
- OpenUrlMoniker(nil);
-
- // Initalize drag and drop
- gDragDrop = CDragDrop::NewDragDrop(AppDataPtr->Window);
-
- // Create the one and only container
- // Real container apps will instantiate one of these per container
- gContainer = new CContainer(nil, AppDataPtr->Window);
-
- // Create the one and only site
- // Real container apps will have one of these per figure or object
- gSite = new CSimpleAppSite();
-
- // Add the site to the container
- // Use the site object to manage control enumeration
- gContainer->AddSite(gSite);
-
- // Tell the site about our IContainer interface
- // Pass the IContainer interface and any container-specific data you want
- gSite->SetContainerData((IContainer*) gContainer, AppDataPtr);
-
- }
-
- void ExitApp(void)
- {
- RemoveControl(); // Remove the control
-
- gSite->Release(); // Release the site
- gContainer->Release(); // Release the container
-
- if ( gDragDrop ) // Dispose of drag and drop
- gDragDrop->DisposeDragDrop();
-
- CloseUrlMoniker();
- CoUninitialize(); // Release COM
- }
-
-
- static void toLower(char* src);
- static void toLower(char* src)
- {
- for(char * p = src;*p; p++){
- *p = tolower(*p);
- }
- }
-
- void CreateControlFromArgs(Char8 **inArgNames, Char8 **inArgValues, Int16 inArgCount)
- {
- short i;
- char Test[256];
- short Width = DEFAULT_WIDTH, Height = DEFAULT_HEIGHT;
-
- RemoveControl();
-
- // Set the pseudo OBJECT tag parameters
- // Real apps would likely have an OBJECT tag to pass
- for ( i = 0; i < inArgCount; i++ )
- {
- gSite->AddParam(*(inArgNames+i), *(inArgValues+i));
-
- // See if we can pull the size out
- strcpy(Test, *(inArgNames+i));
- toLower(Test);
-
- if ( strcmp(Test, "width") == 0 )
- Width = atoi(*(inArgValues+i));
- else if ( strcmp(Test, "height") == 0 )
- Height = atoi(*(inArgValues+i));
- }
-
- // Adjust the size of the control rect
- gControlRect.right = gControlRect.left + Width;
- gControlRect.bottom = gControlRect.top + Height;
-
- // Ask the site to create the control
- // Request that the control be created, downloaded if necessary
- gSite->CreateControl(true);
-
- // Get the IControl interface for the control
- gSite->QueryInterface(IID_IControl, &gControl);
-
- // Add the only DrawContext to the control
- // Some apps may have multiple contexts in which this site is displayed
- DoContextChange(WindowContextID, AddContext);
- if (gAppData.Offscreen)
- DoContextChange(OffscreenContextID, AddContext);
-
- // Activate the context
- DoContextChange(WindowContextID, ActivateContext);
- }
-
- void CreateControlFromFileSpec(FSSpecPtr inFileSpec)
- {
- RemoveControl();
-
- Boolean8 GotArgs = false;
- Char8 **ArgNames;
- Char8 **ArgValues;
- Int16 ArgCount;
- Ptr ArgBufferP = NULL;
- Int16 ResFile = ::FSpOpenResFile(inFileSpec, fsRdPerm);
- StringHandle StrHandle = NULL;
-
- if (::ResError() == noErr)
- {
- if ((StrHandle = ::GetString(128)) != NULL)
- {
- CArgumentParser Parser;
- Int16 i = 0;
- Int16 Len = **StrHandle;
-
- while (++i <= Len)
- Parser.ProcessChar((*StrHandle)[i]);
- Parser.ProcessChar('\0');
- ::ReleaseResource(Handle(StrHandle));
- GotArgs = Parser.GetArguments(&ArgBufferP,&ArgNames, &ArgValues, &ArgCount);
- }
- ::CloseResFile(ResFile);
- }
-
- // for now we need to close the res file before we instantiate the control
- // so that the resource fork chain is straight forward. I hope to fix this.
-
- if (GotArgs)
- {
- Handle BaseURL = FileURLFromFileSpec(inFileSpec);
- if ( BaseURL )
- {
- ::HLock(BaseURL);
- gSite->SetBaseURL(*BaseURL);
- ::HUnlock(BaseURL);
- ::DisposeHandle(BaseURL);
- }
-
- CreateControlFromArgs(ArgNames, ArgValues, ArgCount);
- ::DisposePtr(ArgBufferP);
- }
- else
- ::SysBeep(0);
- }
-
-
- void RemoveControl(void)
- {
- if ( gControl )
- {
-
- // Remove the only context we added
- DoContextChange(1, RemoveContext);
-
- gControl->Release(); // Release our reference on IControl
- gSite->DestroyControl(); // Destroy the control
-
- gControl = nil;
- }
- }
-
-
- void DoContextChange(UInt32 ContextID , ContextCommand Command)
- {
- DrawContext Context = { BeginPortType };
-
- if ( gControl )
- {
- // Add the only DrawContext to the control
- // Some apps may have multiple contexts in which this site is displayed
- gSite->AcquireContext(ContextID, &Context);
- ::EraseRect(&(Context.Port->portRect));
- if (Context.PortType == QDWindowPortType)
- ::InvalRect(&(Context.Port->portRect));
- gSite->ReleaseContext(&Context);
- gControl->OnContextChange(ContextID, Command);
- }
- }
-